import math,pprint

class Bot():
    
    
    def __init__(self,idNo,data):
        self.id=idNo
        self.speed=data[0] #get speed
        self.location=data[1:]
        self.range=10000
        self.densityTable={0:0}
        # print location[:1]
        #print location[1:]
        self.ownTable={idNo:(self.speed,self.location)}
        self.token=False
        self.iteration=0
        self.tableIdList=[]
        self.densityIteration=0
        self.tempDensityTable={}
        
        
       
        
    def createTable(self,otherTable):
        existIdList=[]
        #notUpdatedIdList=[]
        self.iteration=self.iteration+1
        for idNo in otherTable:
            if self.isExistKey(self.ownTable, idNo):
                if not(self.ownTable.get(idNo)[1][0]==otherTable.get(idNo)[1][0]):
                    existIdList.append(idNo)
                
            self.ownTable[idNo]=otherTable[idNo]
        self.ownTable[self.id]=(self.speed,self.location)
        if not(self.id in existIdList):
            existIdList.append(self.id)
        #print self.ownTable
        self.tableIdList.append(existIdList)
        #self.refreshTable()
        if self.iteration==5:
            self.refreshTable()
            self.tableIdList[:]=[]
            self.iteration=0
            
        #print self.ownTable
        
    
    def refreshTable(self):
        survivors=[]
#        for noList in self.tableIdList:
#            print noList,self.iteration
        #for i in range(3,6):
        i=3
        for j in range(0,len(self.tableIdList[i])):
            if self.tableIdList[i][j] in self.tableIdList[i] and self.tableIdList[i][j] in self.tableIdList[i+1]:
                survivors.append(self.tableIdList[i][j])
            
        for id,dic in self.ownTable.items():
            if not(id in survivors):
                del self.ownTable[id]
            
                
            
    def isExistKey(self,dic, key):
        return dic.has_key(key)
        
    def findAverageSpeed(self):
        speeds=dict(self.ownTable.values()).keys()
        return (float(sum(speeds))/len(speeds))
    
    def findMedianLocation(self):
        loc=dict(dict(self.ownTable.values()).values())
        locX=sorted(loc.keys())
        locY=sorted(loc.values())
        if len(locX)%2==1:
            x=locX[((len(locX)+1)/2)-1]
        else:
            x=locX[len(locX)/2]
        return x,loc[x]
    
    def findNumberOfVehicles(self):
        return len(self.ownTable)
    
    def getTable(self):
        return self.ownTable
    
    def inputColonyDetails(self,details,edges):
        location=details[1]#get the location of the colony
        
        if self.isRange(location):
            if details[2]>0:#this should be 5>No_of_Vehicles this is removed for simulation purpesos
                density=self.findTrafficDensity(details[0])#give the speed of the colony
                edge=self.findEdge(location,edges)
                self.createDensityTable(density, edge)
                
    def createDensityTable(self,density,edge):
        self.densityIteration=self.densityIteration+1
        
        if not(edge==None):
            self.tempDensityTable[edge]=density
            if self.isExistKey(self.densityTable, edge):
                if self.densityTable[edge]<density:
                    self.densityTable[edge]=density
            else:
                self.densityTable[edge]=density
        #del self.densityTable[0]
#        print "temp",self.tempDensityTable
#        print "density",self.densityTable,self.densityIteration
        if self.densityIteration==10:
            self.refreshDensityTable()
            self.densityIteration=0
        
        
    def refreshDensityTable(self):
        self.densityTable.clear()
        self.densityTable=self.tempDensityTable.copy()
        self.tempDensityTable.clear()
    
    def findTrafficDensity(self,speed):
        if speed<10:
            return 3
        elif speed<20:
            return 2
        elif speed<50:
            return 1
        
        return 0
    
    def findEdge(self,location,edges):
        for edge,locationList in edges.iteritems():
            for roadLocation in locationList:
                if location[0]==roadLocation[0] and location[1]==roadLocation[1]:
                    return edge
        
    
    def isRange(self,location):
        x=math.pow(math.fabs(self.location[0]-location[0]),2)
        y=math.pow(math.fabs(self.location[1]-location[1]),2)
       
        if (x+y)<self.range:
            return True
        return False

    def drawMap(self):
        if self.isExistKey(self.densityTable, 0):
            del self.densityTable[0]
        return self.densityTable
        
    

        

        
#oTable1={1:(24,(2,48)),4:(30,(2,39)),29:(20,(3,49)),45:(20,(1,49))}
#oTable2={34:(24,(2,48)),4:(30,(2,39)),2:(20,(3,49)),5:(20,(1,49))}
#oTable3={41:(24,(2,48)),4:(30,(2,39)),229:(20,(3,49)),435:(20,(1,49))}
#oTable4={13:(24,(2,48)),4:(30,(2,39)),239:(20,(3,49)),456:(20,(1,49))}
#oTable5={13:(24,(2,48)),4:(30,(2,39)),249:(20,(3,49)),455:(20,(1,49))}
#oTable6={13:(24,(2,48)),4:(30,(2,39)),245:(20,(3,49)),2245:(20,(1,49))}
#oTable7={221:(24,(2,48)),4:(30,(2,39)),294:(20,(3,49)),4533:(20,(1,49))}
#x=Bot(23,(12,3,4))
#x.ownTable={1:(23,(34,45)),2:(34,(30,40)),3:(28,(26,44)),4:(33,(34,56))}
#
#x.createTable(oTable1)
#x.createTable(oTable2)
#x.createTable(oTable3)
#x.createTable(oTable4)
#x.createTable(oTable5)
#x.createTable(oTable6)
#print x.findAverageSpeed();
#print x.ownTable 
#print x.findMedianLocation()
#x.inputColonyDetails([34,(0,13),45],{'1 -> 7': [[3, 14], [4, 14], [5, 14], [6, 14], [7, 14], [8, 14], [9, 14], [10, 14], [11, 14], [12, 14], [13, 14], [14, 14], [14, 13], [14, 12], [14, 11]], '1 -> 0': [[1, 14], [0, 14], [0, 13], [0, 12], [0, 11], [0, 10], [0, 9], [0, 8], [0, 7], [0, 6], [0, 5], [0, 4], [0, 3], [0, 2], [0, 1], [0, 0], [1, 0]], '0 -> 2': [[2, 1], [2, 2]], '0 -> 1': [[1, 0], [0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6], [0, 7], [0, 8], [0, 9], [0, 10], [0, 11], [0, 12], [0, 13], [0, 14], [1, 14]], '1 -> 3': [[2, 13], [2, 12], [2, 11], [2, 10], [2, 9], [2, 8], [3, 8]], '7 -> 6': [[13, 10], [12, 10], [11, 10]], '4 -> 2': [[5, 2], [4, 2]], '7 -> 5': [[14, 9], [14, 8], [14, 7], [14, 6], [14, 5], [14, 4], [13, 4], [12, 4], [11, 4]], '4 -> 0': [[6, 1], [6, 0], [5, 0], [4, 0], [3, 0]], '4 -> 5': [[6, 3], [6, 4], [7, 4], [8, 4], [9, 4]], '6 -> 5': [[10, 9], [10, 8], [10, 7], [10, 6], [10, 5]], '0 -> 4': [[3, 0], [4, 0], [5, 0], [6, 0], [6, 1]], '3 -> 2': [[4, 7], [4, 6], [3, 6], [3, 5], [3, 4], [3, 3]], '2 -> 4': [[3, 1], [3, 0], [4, 0], [5, 0], [6, 0], [6, 1], [[4, 2], [5, 2]]], '7 -> 1': [[14, 11], [14, 12], [14, 13], [14, 14], [13, 14], [12, 14], [11, 14], [10, 14], [9, 14], [8, 14], [7, 14], [6, 14], [5, 14], [4, 14], [3, 14]], '2 -> 0': [[2, 2], [2, 1]], '2 -> 3': [[3, 3], [3, 4], [3, 5], [3, 6], [4, 6], [4, 7]]})#1,2
#x.inputColonyDetails([23,(3,3),45],{'1 -> 2':[[1,2],[3,4],[5,6]],'2 -> 4':[[3,4],[6,5]]})#3,3
#x.inputColonyDetails([56,(4,3),45])#4,1
#x.inputColonyDetails([3,(1,3),45])#1,6
#x.inputColonyDetails([392,(2,3),45])#2,0
#x.inputColonyDetails([65,(3,3),45])#3,1
#x.inputColonyDetails([23,(2,4),45])#2,3
#x.inputColonyDetails([56,(2,3),45])#2,1
#x.inputColonyDetails([67,(1,3),45])#1,1
#x.inputColonyDetails([392,(4,3),45])#4,2
#del x.densityTable[0]
#print x.drawMap()
#c=[3,4,5,6,7]
#if 9 in c:
#    print "ok"
#else:
#    print "no"

'''if x.find_value(table, 1):
    print "ok"
else:
    print "not ok"'''

'''if self.find_value(self.ownTable, id):
                self.ownTable[id]=otherTable[id]
            else:
                self.ownTable[id]'''